package infoManage.service;

import infoManage.dao.StudentDao;
import infoManage.domain.Student;

public class StudentService {
    // DAO(Data Access Object) 数据访问对象（库管）
    StudentDao studentDao = new StudentDao();

    public boolean isExists(String id) { // 判断学号是否存在
        Student[] list = studentDao.findAllStudents();
        boolean flag = false;
        for (int i = 0; i < list.length; i++) {
            Student stu = list[i];
            if(stu != null && stu.getId().equals(id)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    public boolean addStudent(Student stu) { // 添加学生信息
        // 将学生对象, 传递给StudentDao 库管中的addStudent方法
        // 将返回的boolean类型结果, 返还给StudentController
        return studentDao.addStudent(stu);
    }

    public Student[] findAllStudent() { // 查看学生信息
        Student[] list = studentDao.findAllStudents();
        boolean isNull = false;
        for (int i = 0; i < list.length; i++) {
            if(list[i] != null) {
                isNull = true;
                break;
            }
        }
        if(isNull) {
            return list;
        } else {
            return null;
        }
    }

    public void deleteStudent(String id) { // 删除学生信息
        studentDao.deleteStudent(id);
    }

    public void editStudent(Student student) {
        studentDao.editStudent(student);
    }
}
